﻿using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using UtilZ.Dotnet.Ex.Cache;
using UtilZ.Dotnet.Ex.ZLog;

namespace UtilZ.Dotnet.Ex.Base
{
    /// <summary>
    /// EventWaitHandle自动管理类
    /// </summary>
    public class EventWaitHandleManager
    {
        private readonly static ObjectEventWaitHandleManager _waitHandleManager;

        static EventWaitHandleManager()
        {
            _waitHandleManager = new ObjectEventWaitHandleManager();
        }


        /// <summary>
        /// 创建并添加EventWaitHandle
        /// </summary>
        /// <param name="id">EventWaitHandle的唯一标识</param>
        /// <param name="expirationMilliseconds">缓存项有效时间,小于等于0永不过期,单位/毫秒</param>
        /// <param name="tag">Tag</param>
        /// <returns>创建的EventWaitHandle</returns>
        public static EventWaitHandleInfo Create(object id, int expirationMilliseconds = -1, object tag = null)
        {
            return _waitHandleManager.Create(id, expirationMilliseconds, tag);
        }

        /// <summary>
        /// 添加外部创建的EventWaitHandle
        /// </summary>
        /// <param name="id">EventWaitHandle的唯一标识</param>
        /// <param name="eventWaitHandle">EventWaitHandle</param>
        /// <param name="expirationMilliseconds">缓存项有效时间,小于等于0永不过期,单位/毫秒</param>
        /// <param name="tag">Tag</param>
        public static EventWaitHandleInfo Create(object id, EventWaitHandle eventWaitHandle, int expirationMilliseconds = -1, object tag = null)
        {
            return _waitHandleManager.Create(id, eventWaitHandle, expirationMilliseconds, tag);
        }

        /// <summary>
        /// 添加或更新外部创建的EventWaitHandle,如果id存在则替换并返回旧的项,否则则添加返回null
        /// </summary>
        /// <param name="id">EventWaitHandle的唯一标识</param>
        /// <param name="eventWaitHandle">EventWaitHandle</param>
        /// <param name="expirationMilliseconds">缓存项有效时间,小于等于0永不过期,单位/毫秒</param>
        /// <param name="tag">Tag</param>
        /// <returns>旧的EventWaitHandle</returns>
        public static EventWaitHandle CreateOrUpdate(object id, EventWaitHandle eventWaitHandle, int expirationMilliseconds = -1, object tag = null)
        {
            return _waitHandleManager.CreateOrUpdate(id, eventWaitHandle, expirationMilliseconds, tag);
        }

        /// <summary>
        /// 获取EventWaitHandle,获取成功返回获取EventWaitHandle;失败返回null
        /// </summary>
        /// <param name="id">EventWaitHandle的唯一标识</param>
        /// <returns>EventWaitHandle</returns>
        public static EventWaitHandle GetEventWaitHandle(object id)
        {
            return _waitHandleManager.GetEventWaitHandle(id);
        }

        /// <summary>
        /// 获取EventWaitHandleInfo
        /// </summary>
        /// <param name="id">EventWaitHandle的唯一标识</param>
        /// <returns>EventWaitHandleInfo</returns>
        public static EventWaitHandleInfo GetEventWaitHandleInfo(object id)
        {
            return _waitHandleManager.GetEventWaitHandleInfo(id);
        }

        /// <summary>
        /// 移除并返回EventWaitHandle
        /// </summary>
        /// <param name="id">EventWaitHandle的唯一标识</param>
        /// <returns>EventWaitHandle</returns>
        public static EventWaitHandleInfo RemoveEventWaitHandle(object id)
        {
            return _waitHandleManager.RemoveEventWaitHandle(id);
        }
    }
}
